package com.allianz.webapps.controllers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.allianz.frame.utils.ApplicationContextUtil;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.https.interfaces.type004.Type004Body;
import com.allianz.webapps.https.interfaces.type004.Type004Service;
import com.allianz.webapps.https.interfaces.type004.response.Type004ResponseAll;
import com.allianz.webapps.https.interfaces.type004.response.Type004ResponseBody;
import com.allianz.webapps.https.interfaces.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
//http://localhost:8080/pad_marketing/main/type004AllList.action
@Controller
@RequestMapping(value="main")
public class Type004GetAllListController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ObjectUtil objectUtil;
	@Autowired
	private ObjectMapper objectMapper;
	@RequestMapping(value="type004AllList")
	@ResponseBody
	public Map<String,String> getAllList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HttpSession hs = request.getSession();
		User u = (User)hs.getAttribute("username");
		String result = objectUtil.requestParam(u,"pkg.getAllList");
		
		Map<String,String> retMap = new HashMap<String,String>();
		String result_map = "";
		try {
			Type004ResponseAll ra = objectMapper.readValue(result,Type004ResponseAll.class);
			if("F".equals(ra.getStatus())){
				retMap.put("error",ra.getErrMsg());
				return retMap;
			}
			List<Type004ResponseBody> rb = objectMapper.readValue(ra.getBody(), new TypeReference<List<Type004ResponseBody>>() {});
			Map<String,List<Type004ResponseBody>> map = new HashMap<String,List<Type004ResponseBody>>();
			for(Type004ResponseBody t : rb ){
				String key = t.getCategory();
				if(map.containsKey(key)){
					List<Type004ResponseBody> tempList = map.get(key);
					tempList.add(t);
				}else{
					List<Type004ResponseBody> tbs = new ArrayList<Type004ResponseBody>();
					tbs.add(t);
					map.put(key,tbs);
				}
			}
			
			try {
				result_map = objectMapper.writeValueAsString(map);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		retMap.put("message", result_map);
		return retMap;
	}
	
	///pad_marketing/main/type004redirectlistViewAll.action
	@RequestMapping(value="type004redirectlistViewAll")
	public ModelAndView redirect(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("views/page/listViewAll");
	}
}